package com.bkhech.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.bkhech.grace.result.GraceJSONResult;
import com.bkhech.pojo.bo.CommentBO;
import com.bkhech.pojo.vo.CommentVO;
import com.bkhech.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class CommentController {

    private final CommentService commentService;

    @PostMapping("/create")
    public GraceJSONResult<CommentVO> create(@RequestBody CommentBO commentBO) {
        CommentVO commentVO = commentService.createComment(commentBO);
        return GraceJSONResult.ok(commentVO);
    }

    @PostMapping("/query")
    public GraceJSONResult<Collection<CommentVO>> query(String friendCircleId) {
        if (StringUtils.isBlank(friendCircleId)) {
            return GraceJSONResult.error();
        }
        Collection<CommentVO> commentVOs = commentService.queryAll(List.of(friendCircleId));
        return GraceJSONResult.ok(commentVOs);
    }

    @PostMapping("/delete")
    public GraceJSONResult<Void> delete(String commentUserId, String commentId, String friendCircleId) {
        if (StringUtils.isBlank(commentUserId) || StringUtils.isBlank(commentId) || StringUtils.isBlank(friendCircleId)) {
            return GraceJSONResult.error();
        }
        commentService.deleteComment(commentUserId, commentId, friendCircleId);
        return GraceJSONResult.ok();
    }
}
